package cn.akaba.yun.appCls;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class AppVar {
    private static AppVar instance;
    public static final String WEB_DOMAIN = "http://localhost:8080";
//    public static final String WEB_DOMAIN = "http://chiyuxinyi.com";
//    public static final String WEB_DOMAIN = "http://akaba.cn";
    public static final String BAI_DU_AK = "QkBI71zjZH0qTU3aIrcQ5VEc";
    public static final String BAI_DU_SK = "9R6dqwRu3CUMQyvA9XOLUFcdBvnN7rFz";
    public static final String AUTH_BACK_URL = AppVar.WEB_DOMAIN + "/baidu/auth";
//    public static final String TOKEN_BACK_URL = AppVar.WEB_DOMAIN + "/baidu/token";
    public static final String TOKEN_BACK_URL = "http://chiyuxinyi.com" + "/baidu/token";

    public static final String URL_TPL_AUTH = "http://openapi.baidu.com/oauth/2.0/authorize?response_type=code&client_id=${ak}&redirect_uri=${redirectUrl}&scope=basic,netdisk&display=${display}&qrcode=1&force_login=1";

    public static final String URL_TPL_TOKEN = "https://openapi.baidu.com/oauth/2.0/token?grant_type=authorization_code&code=${code}&client_id=${ak}&client_secret=${sk}&redirect_uri=${redirectUrl}";

    public static final String URL_TPL_TOKEN_REFRESH = "https://openapi.baidu.com/oauth/2.0/token?grans_type=refresh_token&refresh_token=${refreshToken}&client_id=${ak}&client_secret=${sk}";

    public static enum AUTH_DISPLAY {
        PAGE,
        POPUP,
        DIALOG,
        MOBILE,
        TV,
        PAD
    };

    private AppVar() {
    }

    public void doSomething() {
        System.out.println("do something");
    }

    public static String getUrlTplAuth() {
        return getUrlAuth(AUTH_DISPLAY.PAGE);
    }

    public static String getUrlAuth(AUTH_DISPLAY display) {
        Map<String, Object> map = new HashMap<>();
        map.put("ak", AppVar.BAI_DU_AK);
        map.put("sk", AppVar.BAI_DU_SK);
        map.put("redirectUrl", URLEncoder.encode(AppVar.AUTH_BACK_URL, StandardCharsets.UTF_8));
        map.put("display", display.name().toLowerCase());
        return AppStringTool.format(AppVar.URL_TPL_AUTH, map);
    }

    public static String getUrlAccessToken(String code) {
        Map<String, Object> map = new HashMap<>();
        map.put("ak", AppVar.BAI_DU_AK);
        map.put("sk", AppVar.BAI_DU_SK);
        map.put("code", code);
        map.put("redirectUrl", URLEncoder.encode(AppVar.AUTH_BACK_URL, StandardCharsets.UTF_8));
        return AppStringTool.format(AppVar.URL_TPL_TOKEN, map);
    }

    public static String getUrlAccessTokenRefresh(String refreshToken) {
        Map<String, Object> map = new HashMap<>();
        map.put("ak", AppVar.BAI_DU_AK);
        map.put("sk", AppVar.BAI_DU_SK);
        map.put("refreshToken", refreshToken);
        map.put("redirectUrl", URLEncoder.encode(AppVar.AUTH_BACK_URL, StandardCharsets.UTF_8));
        return AppStringTool.format(AppVar.URL_TPL_TOKEN_REFRESH, map);
    }

    public static AppVar getInstance() {
        if (AppVar.instance == null) {
            AppVar.instance = new AppVar();
        }
        return AppVar.instance;
    }
}
